<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of menu
 *
 * @author Dmitry
 */
require_once 'app/access/db.php';
class Models_Menu extends Access_Db {
    //put your code here
    protected $query ;
    var $id ;
    private $registry ;
    function __construct($registrty) {
        $this->registry = $registrty ;
    }

    public function getMenu()
    {
        $this->query = "SELECT ID, name FROM yyMenu WHERE visible=1" ;
        $result = $this->executeQuery($this->query) ;
        while ($row = mysql_fetch_array($result))
        {
            $menu[$row['ID']] = $row['name'] ;
        }
        return $menu ;
    }

    public function getSubMenu($id)
    {
        settype($id, 'integer') ;
        $this->query = "SELECT yyProducts.categoryID, yyCategories.name 
                FROM yyCategories, yyProducts 
                WHERE yyCategories.visible=1 and yyCategories.menuID=$id and
                yyCategories.ID=yyProducts.categoryID" ;
        $result = $this->executeQuery($this->query) ;
        while ($row = mysql_fetch_array($result))
        {
            $subMenu[$row['categoryID']] = $row['name'] ;
        }
        return $subMenu ;
    }
    
    public function getSubMenuUri($id)
    {
        settype($id, 'integer') ;
        $result = $this->executeQuery("SELECT uri FROM yyCategories WHERE ID=$id") ;
        $row = mysql_fetch_assoc($result) ;
        return $row['uri'] ;
    }
    
    public function getMenuUri($id)
    {
        settype($id, 'integer') ;
        $result = $this->executeQuery("SELECT uri FROM yyMenu WHERE ID=$id") ;
        $row = mysql_fetch_assoc($result) ;
        return $row['uri'] ;
    }
    
    public function showMenu()
    {
        echo '<ul id="menu">' ;
        $menu = $this->getMenu() ;
        foreach ($menu as $menuID => $menuName)
        {
            echo '<li><a href="'. 'products/all/' .$menuID.'">' . $menuName . '</a>' ;
            echo '<ul id="submenu">' ;
            $subMenu = $this->getSubMenu($menuID) ;
            foreach ($subMenu as $subMenuID => $subMenuName)
            {
                echo '<li><a href="'. $this->registry->get('sitePath') .
                       'products/category/' . $subMenuID.'">' . $subMenuName . '</a></li>';
            }
            echo '</ul></li>' ;
        }
        echo '</ul>' ;
        
    }
}
?>
